#include "pcik_bot.h"

PcikBot* pcik_bot_new()
{
	PcikBot* bot = (PcikBot*)malloc(sizeof(PcikBot));
	
	return bot;
}
void pcik_bot_init(PcikBot* bot)
{
	if (NULL == bot)
		return;

	bot->astar = astar_new();
	astar_set_heavy_diagonals(bot->astar, false);
	astar_init_map(bot->astar, 2048, 1000, (Astar_IsMovable)&is_movable, (Astar_GetMoveCost)&get_move_cast);

	bot->path = NULL;
	bot->node = NULL;
}
void pcik_bot_delete(PcikBot* bot)
{
	if (NULL != bot->astar)
		astar_delete(bot->astar);

	if (NULL != bot->path)
		astar_delete_path(bot->path);

	free(bot);
}
void pcik_bot_update(PcikBot* bot)
{
	if (NULL != bot->path && NULL != bot->node)
	{
		MapPoint pos;
		get_hero_map_point(&pos);

		if (abs(bot->node->x - pos.x) <= 5 && abs(bot->node->y - pos.y) <= 5)
		{
			printf("move to [%d,%d]\n", bot->node->x, bot->node->y);
			run(get_hero_id(), bot->node->x, bot->node->y);
			bot->node = (astar_node*)bot->node->next;
			if (NULL == bot->node)
			{
				astar_delete_path(bot->path);
				bot->path = NULL;
			}
		}
	}
}
bool pcik_bot_auto_move(PcikBot* bot, MapPointX x, MapPointY y)
{
	MapPoint pos;
	get_hero_map_point(&pos);
	bot->path = astar_find_path(bot->astar, pos.x, pos.y, x, y, Euclidean, 1200000);
	bot->node = bot->path;
	bot->dst_pos.x = x;
	bot->dst_pos.y = y;
	printf("path = 0x%08X\n", bot->path);
	
	return NULL != bot->path;
}